package com.qianyu.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.ComplementSettlement;
import com.qianyu.project.entity.ComplementSettlementList;
import com.qianyu.project.mapper.ComplementSettlementMapper;
import com.qianyu.project.service.IComplementSettlementListService;
import com.qianyu.project.service.IComplementSettlementService;
import com.qianyu.project.utils.PageUtil;
import com.qianyu.project.vo.ComplementSettlementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ComplementSettlementServiceImpl extends ServiceImpl<ComplementSettlementMapper, ComplementSettlement> implements IComplementSettlementService {
    @Autowired
    private ComplementSettlementMapper complementSettlementMapper;

    @Autowired
    private IComplementSettlementListService complementSettlementListService;

    @Override
    public String getMaxId() {
        String id = complementSettlementMapper.getMaxId();
        if(id == null){
            id = DateUtil.today().replaceAll("-", "") + "000";
        }
        return id;
    }

    @Transactional
    @FlowBegin("complement_settlement")
    @Override
    public Integer saveBoth(@FlowEntry("id") ComplementSettlement settleForm, List<ComplementSettlementList> settleDetails) {
        settleForm.setApproveStatus("1");
        complementSettlementMapper.insert(settleForm);
        complementSettlementListService.saveBatch(settleDetails);
        return 1;
    }

    @Override
    public IPage<ComplementSettlementVo> getList(Map<String, String> params) {
        return complementSettlementMapper.getList(PageUtil.createPage(params), params);
    }

    @Override
    public ComplementSettlementVo selectOne(String id) {
        return complementSettlementMapper.getOne(id);
    }

}
